package k.b.s;

import java.io.InputStream;
import java.util.ArrayList;

import k.b.s.model.ChannelRowModel;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RssRegist extends ListActivity implements OnClickListener {

	ArrayList<ChannelRowModel> rowModels;
	TextView textHttp;
	EditText editHttp;
	RssRegistlist adpter;
	private String deleteposition;
//	private String newHttp;
	TextView textTarget;
//	TextView textNow;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rssmanager);
		editHttp = (EditText)findViewById(R.id.edit_http);
		textTarget = (TextView)findViewById(R.id.text_target);
//		textNow = (TextView)findViewById(R.id.text_now);

	}

	public void onResume() {
		super.onResume();

		this.adpter = new RssRegistlist(this);
		setListAdapter(adpter);
		rowModels = OperationDB.getChannelTB(this);
		int num = rowModels.size();

		for (int i = 0; i < num; i++) {
			ChannelRowModel row = new ChannelRowModel();
			row.setChannel(rowModels.get(i).getChannel());
			adpter.add(row);
		}
	}

	public void onClickBack(View v){
		finish();
	}


	public void onClicHttpAdd(View v) {
		try {
			if(OperationDB.isNotFull(this)){
				String newHttp = editHttp.getText().toString();
				SQLiteDatabase database = OperationDB.getWDB(this);
		    	InputStream in = HttpGetter.getContent(newHttp);
		    	ChannelRowModel rowModel = ChannelXmlHelper.getInstance().parseSax(in,newHttp);
		    	OperationDB.addChannel(rowModel,database, this );
		    	Toast.makeText(this, "正常に追加できました", Toast.LENGTH_SHORT).show();
		    	onResume();
			}else{
				Toast.makeText(this, "追加できませんでした", Toast.LENGTH_SHORT).show();
			}

		}catch (SQLException e) {
			Toast.makeText(this, "エラーが発生したので\n処理を中断しました", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}

	public void onClickHttpDelete(View v){

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("  Realy ?  ");
		builder.setIcon(android.R.drawable.ic_menu_mylocation);
		builder.setTitle("HTTP-TARGET-CLEAR");
		builder.setPositiveButton("YES", new DialogInterface.OnClickListener(){
	        	public void onClick(DialogInterface dialog, int which){

	        		Toast.makeText(RssRegist.this, " Delete clear ", Toast.LENGTH_SHORT).show();
	        		OperationDB.deleteChannel(deleteposition, RssRegist.this);
	        		Intent intent = new Intent(RssRegist.this, RssRegist.class);
	        		startActivity(intent);
	        		finish();
	        	}
	        });
	        builder.setNegativeButton("NO", new DialogInterface.OnClickListener(){
	        	public void onClick(DialogInterface dialog, int which){
	        		Toast.makeText(RssRegist.this, " No Delete", Toast.LENGTH_SHORT).show();
	        	}
	        });

	        builder.show();
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		ChannelRowModel item =  adpter.getItem(position);
		this.deleteposition = item.getChannel();
		editHttp.setText(deleteposition);
//		textNow.setText("Now Target");

	}

	class RssRegistlist extends ArrayAdapter<ChannelRowModel> {

		public RssRegistlist(Context context) {
			super(context, R.layout.rss_mlist);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View row = convertView;
			ChannelRowModel channnelRow = getItem(position);

			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.rss_mlist, null);
			}

			if (channnelRow != null) {

				textHttp = (TextView) row.findViewById(R.id.http_text);

				if (textHttp != null) {
					textHttp.setText(channnelRow.getChannel());

				}
			}
			return row;
		}
	}

	public void h1(View v) {
		editHttp.setText("http://www.oesf.jp/modules/news/index.php?page=rss");
	}

	public void h2(View v) {
		editHttp.setText("http://sjz.jp/blog/feed/");
	}

	public void h3(View v) {
		editHttp.setText("http://www.4gamer.net/games/051/G005120/contents.xml");
	}

	public void h4(View v) {
		editHttp.setText("http://shizuoka-bicyclemap.net/modules/rss/index.php?page=xml&style=rss20");
	}

	public void onClick(DialogInterface arg0, int arg1) {
	}
}
